package com.ddogring.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddogring.business.domain.User;
import com.ddogring.business.mapper.UserMapper;
import com.ddogring.business.service.IUserService;
import com.ddogring.common.constant.UserConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @BelongsProject: eshop
 * @BelongsPackage: com.ddogring.business.service.impl
 * @Author: DdogRing
 * @CreateTime: 2023-01-30  11:17
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * @description: 校验用户名唯一性
     * @author: DdogRing
     * @date: 2023/1/31 09:42
     * @param: [username]
     * @return: java.lang.String
     **/
    @Override
    public String checkUserNameUnique(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username).eq("del_flag", "0");
        int count = userMapper.checkUserNameUnique(wrapper);
        return count > 1 ? UserConstants.NOT_UNIQUE : UserConstants.UNIQUE;
    }

    /**
     * @description: 校验手机号唯一性
     * @author: DdogRing
     * @date: 2023/1/31 09:44
     * @param: [user]
     * @return: java.lang.String
     **/
    @Override
    public String checkPhoneUnique(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", user.getPhone()).eq("del_flag", "0");
        int count = userMapper.checkPhoneUnique(wrapper);
        return count == 0 ? UserConstants.UNIQUE : UserConstants.NOT_UNIQUE;
    }

    /**
     * @description: 校验邮箱唯一性
     * @author: DdogRing
     * @date: 2023/1/31 09:50
     * @param: [user]
     * @return: java.lang.String
     **/
    @Override
    public String checkEmailUnique(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("email", user.getEmail()).eq("del_flag", "0");
        int count = userMapper.checkEmailUnique(wrapper);
        return count == 0 ? UserConstants.UNIQUE : UserConstants.NOT_UNIQUE;
    }

    /**
     * @description: 绑定邮箱
     * @author: DdogRing
     * @date: 2023/1/31 09:57
     * @param: [user]
     * @return: int
     **/
    @Override
    public int bind(User user) {
        return userMapper.bind(user);
    }
}
